from pathlib import Path
import csv
from datetime import datetime

import matplotlib.pyplot as plt

path = Path('sitka_weather_2021_simple.csv')
lines = path.read_text().splitlines()

reader = csv.reader(lines)
header_row = next(reader)

# for index,column_header in enumerate(header_row):
#     print(index,column_header)

# 提取日期和最高温度
dates,highs = [],[]
for row in reader:
    current_date = datetime.strptime(row[2],'%Y-%m-%d')
    high = int(row[4])
    dates.append(current_date)
    highs.append(high)

# 根据数据绘图
plt.style.use('classic')
fig, ax = plt.subplots()
ax.plot(dates,highs,color='red')

# 设置绘图的格式
ax.set_title('Daily High Temperatures, 2021',fontsize=20)
ax.set_xlabel('Day',fontsize=16)
# 调用fig.autofmt_xdate()来绘制倾斜的日期标签，以免它们彼此重叠
fig.autofmt_xdate()
ax.set_ylabel("Temperature(F)",fontsize=16)
ax.tick_params(labelsize=16)

plt.show()
